
Friend Class frmInterHeight
    Private m_tables As TableHolder
    Private m_table As Table
    Private m_scale As Scale
    Private m_scaleTable As Table
    Private m_ArrayS()() As Byte
    Private m_ArrScale() As Byte

    Friend Sub New(ByRef Tables As TableHolder, ByRef Table As Table, ByRef Array_()() As Byte, ByRef Axis As Scale)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        m_tables = Tables
        m_table = Table
        m_ArrayS = Array_
        m_scale = Axis

        If m_scale.Address > 0 Then
            Dim f_str As String = m_table.Name
            If Not IsNothing(m_scale.Description) AndAlso Not IsNothing(m_scale.Description.Description) Then f_str &= " - " & m_scale.Description.Description
            m_scaleTable = New Table(0, 1, m_table.adjustedHeight, f_str)
            m_scaleTable.setAxis(False, False, m_table.HeightInvertDisplay, DisplayMode.BK_DISP_ADJUSTED, DisplayScheme.BK_SCH_BYTE)
            m_scaleTable.setDisplay(m_scale.Multiplier, m_scale.preDivider, m_scale.Offset, 0)
            Me.Text &= " - " & m_table.Name
            ReDim m_ArrScale(m_scaleTable.adjustedHeight - 1)
            array_copy(m_ArrayS(0), m_scale.Address, m_ArrScale, 0, m_scaleTable.adjustedHeight)
            eGrid.populate(m_scaleTable, m_ArrScale)
            Me.Size = New Size(Me.Size.Width, 150 + (m_scaleTable.adjustedHeight * eGrid.Item(0, 0).Size.Height))
        End If
    End Sub

    Private Sub dgRPM_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles eGrid.KeyPress
        Dim f_add As Double
        Dim f_mult As Double
        Dim f_str As String = ""
        Dim state As Controls.libGrid = CType(sender, DataGridView)
        Dim f_dialog As frmBDialog
        If Not IsNothing(m_scaleTable) And Not IsNothing(m_ArrScale) Then
            If e.KeyChar = Chr(113) Or e.KeyChar = Chr(97) Then
                If e.KeyChar = Chr(113) Then f_add = m_scaleTable.Value Else f_add = -m_scaleTable.Value
                For Each cell As Controls.libGridCell In state.SelectedCells
                    cell.Value += f_add
                Next
            ElseIf e.KeyChar >= "0" And e.KeyChar <= "9" Then
                f_dialog = New frmBDialog("Enter new value for selection", "New Value", e.KeyChar)
                If f_dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                    f_str = f_dialog.Value
                End If
                If Trim(f_str) = "" Then Exit Sub
                f_mult = Val(f_str)
                For Each cell As Controls.libGridCell In state.SelectedCells
                    cell.Value = f_mult
                Next
            ElseIf e.KeyChar = Chr(42) Or e.KeyChar = Chr(45) Or e.KeyChar = Chr(43) Or e.KeyChar = Chr(47) Then
                f_dialog = New frmBDialog("Enter new value for above operator", e.KeyChar)
                If f_dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                    f_str = f_dialog.Value
                End If
                If Trim(f_str) = "" Then Exit Sub
                f_mult = Val(f_str)
                Select Case e.KeyChar
                    Case Chr(42)
                        For Each cell As Controls.libGridCell In state.SelectedCells
                            cell.Value *= f_mult
                        Next
                    Case Chr(45)
                        For Each cell As Controls.libGridCell In state.SelectedCells
                            cell.Value -= f_mult
                        Next
                    Case Chr(43)
                        For Each cell As Controls.libGridCell In state.SelectedCells
                            cell.Value += f_mult
                        Next
                    Case Chr(47)
                        If f_mult <> 0 Then
                            For Each cell As Controls.libGridCell In state.SelectedCells
                                cell.Value \= f_mult
                            Next
                        End If
                End Select
            End If
        End If
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim NoOk As Boolean = False
        Dim liTables As New ArrayList
        Dim objTable As Table

        For i As Integer = 0 To UBound(m_ArrScale) - 2
            If m_ArrScale(i) >= m_ArrScale(i + 1) Then NoOk = True
        Next

        ' Check if the entere scales is valid
        If NoOk Then
            MsgBox("RPM scale is not Valid!", MsgBoxStyle.Critical, "Not Valid!")
            Exit Sub
        End If

        ' Find all tables with the scale associated with it
        For Each tab As Table In m_tables.getTablesAsArray
            If (Not IsNothing(tab.getWidthScale) AndAlso (tab.getWidthScale = m_scale)) Or _
                (Not IsNothing(tab.getHeightScale) AndAlso (tab.getHeightScale = m_scale)) _
            Then
                liTables.Add(tab)
            End If
        Next

        ' Apply new scale to all tables
        If liTables.Count >= 1 Then
            For i As Integer = 0 To liTables.Count - 1
                objTable = CType(liTables(i), Table)
                For a As Integer = 0 To UBound(m_ArrayS)
                    If objTable.interNewScale(m_ArrayS(a), m_ArrScale, m_scale) = False Then GoTo failed
                Next
            Next
        End If
        For a As Integer = 0 To UBound(m_ArrayS)
            If Not m_ArrayS(a) Is Nothing Then
                array_copy(m_ArrScale, 0, m_ArrayS(a), m_scale.Address, UBound(m_ArrScale) + 1)
            End If
        Next
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
        Exit Sub
failed:
        MsgBox("Interpolation Failed for some reason. check your scales and try again")
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.Finalize()
        Me.Close()
    End Sub

End Class
